Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Multithreading → Starting Thread

Multithreading

Starting Thread

Starting a Thread

In Java, creating a thread isn't enough to make it run concurrently. You need to initiate its execution using the start() method. Here's a breakdown of the process and key points to remember:

start() method:

✦ The start() method is a crucial method of the Thread class. ✦ When called on a thread object, it signals the Java runtime environment (JRE) to schedule the thread for execution. ✦ This scheduling involves placing the thread in a pool of runnable threads waiting for CPU time from the operating system. ✦ The operating system decides which thread gets CPU time based on its scheduling algorithm (preemptive or non-preemptive).

How to use?

✦ The start() method can only be called once on a thread object. Attempting to call it multiple times results in an IllegalThreadStateException. ✦ Calling start() directly on a thread object created by extending Thread or implementing Runnable starts the thread. It doesn't execute the run() method within the current thread (usually the main thread). ✦ Once start() is called, the thread transitions from the new state to the runnable state, indicating it's ready to run.

Example (Extending Thread):

Example of start() method in thread - Extend thread public class Main extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread " + Thread.currentThread().getName() + " - Iteration: " + i); } } public static void main(String[] args) { Main thread = new Main(); thread.start(); // Starts the thread's execution concurrently // Main thread code can go here (runs independently) } }

Output

Thread Thread-0 - Iteration: 0 Thread Thread-0 - Iteration: 1 Thread Thread-0 - Iteration: 2 Thread Thread-0 - Iteration: 3 Thread Thread-0 - Iteration: 4

Example (Implementing Runnable):

Example of start() method in thread - implements runnable public class Main implements Runnable { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println("Thread " + Thread.currentThread().getName() + " - Iteration: " + i); } } public static void main(String[] args) { Main runnable = new Main(); Thread thread = new Thread(runnable); thread.start(); // Starts the thread's execution concurrently // Main thread code can go here (runs independently) } }

Output

Thread Thread-0 - Iteration: 0 Thread Thread-0 - Iteration: 1 Thread Thread-0 - Iteration: 2 Thread Thread-0 - Iteration: 3 Thread Thread-0 - Iteration: 4

Important notes: ✦ Calling start() doesn't guarantee immediate execution. The thread scheduling is handled by the operating system. ✦ The main thread and the newly created thread will run concurrently, allowing the program to perform tasks in parallel. ✦ It's essential to understand thread states to manage their lifecycles effectively. Threads might be in a runnable state waiting for CPU time or in a blocked state waiting for a resource. ✦ Starting a thread doesn't automatically make it wait for the main thread to finish. Both threads can potentially execute until completion or termination.

Tutorials